package com.wink_172.activity

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.GestureDetector
import android.view.MotionEvent
import android.view.View
import android.widget.RelativeLayout
import androidx.databinding.DataBindingUtil
import com.wink_172.commons.R
import com.wink_172.commons.databinding.ActivityDynamicLayoutBinding
import com.wink_172.commons.databinding.ActivityFirstBinding
import com.wink_172.library.activity.SmartActivity
import com.wink_172.library.model.Constants
import com.wink_172.library.view.ActionBarView

/**
 * 动态布局
 * */
class DynamicLayoutActivity : SmartActivity(), View.OnTouchListener {

    lateinit var binding: ActivityDynamicLayoutBinding

    companion object {
        open fun startActivity(context: Context) {

            //apply用法 kotlin-P132
            val intent = Intent(context, DynamicLayoutActivity::class.java).apply {
                putExtra(Constants.PARAM_DATA1, "hello01");
            }

            context.startActivity(intent)
        }

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = DataBindingUtil.setContentView(this, R.layout.activity_dynamic_layout)


        initView()
        init()
    }

    fun initView(){

    }

    fun init(){
        val lp=binding!!.layout1.layoutParams as RelativeLayout.LayoutParams
        lp.removeRule(RelativeLayout.CENTER_IN_PARENT)
        lp.addRule(RelativeLayout.CENTER_VERTICAL)
        lp.leftMargin=100


        binding!!.container1.setOnTouchListener(this)
        gd=  GestureDetector(this, mGestureListener)

    }

        override fun onTouch(p0: View, event: MotionEvent): Boolean {
        p0.performClick();

        Log.e(TAG, "onTouch: ====>>"+(event==null)+"    "+(gd==null) )
        return gd!!.onTouchEvent(event);
    }

    private  val TAG = "DynamicLayoutActivity"


    var gd: GestureDetector? =null
     val mGestureListener: GestureDetector.OnGestureListener = object : GestureDetector.SimpleOnGestureListener(){
        val FLING_MIN_DISTANCE = 50
        val FLING_MIN_VELOCITY = 50f

        override fun onShowPress(e: MotionEvent) {
            super.onShowPress(e)
        }
        override fun onLongPress(e: MotionEvent){

        }
        override fun onDown(e: MotionEvent): Boolean{

            return true
        }
        override fun onFling(e1: MotionEvent, e2: MotionEvent, velocityX: Float, velocityY: Float): Boolean{

            if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE
                && Math.abs(velocityX) > FLING_MIN_VELOCITY) {

                AnimActivity.startActivity(activity,0)
                //向左手势
//                overridePendingTransition(R.anim.slide_right_in, R.anim.slide_right_out);

            }
//            else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE
//                && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
//
//                // 切换Activity
//                Toast.makeText(MiddleActivity.this, "向右手势", Toast.LENGTH_SHORT)
//                    .show();
//                overridePendingTransition(R.anim.in_from_left,
//                    R.anim.out_to_right);
//
//            }
            return false
        }
        override fun onScroll(e1: MotionEvent, e2: MotionEvent, distanceX: Float, distanceY: Float): Boolean{

            return false
        }
        override fun onSingleTapUp(e: MotionEvent): Boolean{

            return false
        }
        override fun onDoubleTap(e: MotionEvent): Boolean{

            return false
        }
    }



}